import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_example/get_getbuilder/controller/my_getx_controller.dart';

class MyGetBuilderPage extends StatelessWidget {
  //路由
  static const String route = '/MyGetBuilderPage';

  const MyGetBuilderPage({super.key});

  @override
  Widget build(BuildContext context) {
    final MyGetBuilderController getxBuilderController = MyGetBuilderController();

    return Scaffold(
      appBar: AppBar(title: const Text("Getx_GetBuilder"), actions: [
        IconButton(
          icon: const Icon(Icons.refresh),
          onPressed: () {
             getxBuilderController.updateAll();
            getxBuilderController.update([
              "updateCount",
              "updateString",
              "updateList",
              "updateMap",
              "updateModels",
              "updateRxModels",
              "updateModel",
              "updateIsTrue",
            ]);
          },
        ),
       
      ]),
      body: SingleChildScrollView(
          child: Column(
        children: [
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update Count"),
              subtitle: GetBuilder<MyGetBuilderController>(
                  id: "updateCount",
                  init: getxBuilderController,
                  initState: (_) {
                    debugPrint("initState");
                  },
                  builder: (_) {
                    return Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('count = ${getxBuilderController.count.value}'),
                        Text('count = ${getxBuilderController.count1.value}'),
                      ],
                    );
                  }),
              onTap: () {
                getxBuilderController.updateCount();
                getxBuilderController.update(["updateCount"]);
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update String "),
              subtitle: GetBuilder<MyGetBuilderController>(
                id: "updateString",
                init: getxBuilderController,
                initState: (_) {},
                builder: (_) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('我要买${getxBuilderController.audi.value}'),
                      Text('我要买${getxBuilderController.audi.value}'),
                    ],
                  );
                },
              ),
              onTap: () {
                getxBuilderController.updateAudi();
                getxBuilderController.update(["updateString"]);
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update list"),
              subtitle: GetBuilder<MyGetBuilderController>(
                id: "updateList",
                init: getxBuilderController,
                initState: (_) {},
                builder: (_) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('我要买${getxBuilderController.list.toString()}'),
                      Text('我要买${getxBuilderController.list1.toString()}'),
                    ],
                  );
                },
              ),
              onTap: () {
                //list, 要修改list里面的元素, 才会去更新UI
                getxBuilderController.updateList();
                getxBuilderController.update(["updateList"]);
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update map"),
              subtitle: GetBuilder<MyGetBuilderController>(
                id: "updateMap",
                init: getxBuilderController,
                initState: (_) {},
                builder: (_) {
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('我要买${getxBuilderController.map.toString()}'),
                      Text('我要买${getxBuilderController.map1.toString()}'),
                    ],
                  );
                },
              ),
              onTap: () {
                getxBuilderController.updateMap();
                getxBuilderController.update(["updateMap"]);
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update updateModels"),
              subtitle: GetBuilder<MyGetBuilderController>(
                  id: "updateModels",
                  init: getxBuilderController,
                  initState: (_) {},
                  builder: (_) {
                    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                      const Text("data: getxBuilderController.models",
                          style: TextStyle(color: Colors.black, fontSize: 20, fontWeight: FontWeight.bold)),
                      Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: getxBuilderController.models.map((element) {
                            return Text('我要买${element.name}');
                          }).toList()),
                      const Text("data: getxBuilderController.models1",
                          style: TextStyle(color: Colors.black, fontSize: 20, fontWeight: FontWeight.bold)),
                      Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: getxBuilderController.models1.map((element) {
                            return Text('我要买${element.name}');
                          }).toList()),
                    ]);
                  }),
              onTap: () {
                getxBuilderController.updateModels();
                getxBuilderController.update(["updateModels"]);
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update updateRxModels"),
              subtitle: GetBuilder<MyGetBuilderController>(
                  id: "updateRxModels",
                  init: getxBuilderController,
                  initState: (_) {},
                  builder: (_) {
                    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                      const Text("data: getxBuilderController.rxModels",
                          style: TextStyle(color: Colors.black, fontSize: 20, fontWeight: FontWeight.bold)),
                      Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: getxBuilderController.rxModels.map((element) {
                            return Text('我要买${element.name}');
                          }).toList()),
                      const Text("data: getxBuilderController.rxModels1",
                          style: TextStyle(color: Colors.black, fontSize: 20, fontWeight: FontWeight.bold)),
                      Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: getxBuilderController.rxModels1.map((element) {
                            return Text('我要买${element.name}');
                          }).toList()),
                    ]);
                  }),
              onTap: () {
                getxBuilderController.updateRxModels();
                getxBuilderController.update(["updateRxModels"]);
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update updateModel"),
              subtitle: GetBuilder<MyGetBuilderController>(
                  id: "updateModel",
                  init: getxBuilderController,
                  initState: (_) {},
                  builder: (_) {
                    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                      Text("data model: ${getxBuilderController.model.value.name}"),
                      Text("data model1: ${getxBuilderController.model1.value.name}"),
                    ]);
                  }),
              onTap: () {
                getxBuilderController.updateModel();
                getxBuilderController.update(["updateModel"]);
              },
            ),
          ),
          Card(
            margin: const EdgeInsets.only(left: 20, right: 20, bottom: 10, top: 10),
            child: ListTile(
              title: const Text("update updateIsTrue"),
              subtitle: GetBuilder<MyGetBuilderController>(
                  id: "updateIsTrue",
                  init: getxBuilderController,
                  initState: (_) {},
                  builder: (_) {
                    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                      Text("data isTrue: ${getxBuilderController.isTrue.value}"),
                      Text("data isTrue1: ${getxBuilderController.isTrue1.value}"),
                    ]);
                  }),
              onTap: () {
                getxBuilderController.updateIsTrue();
                getxBuilderController.update(["updateIsTrue"]);
              },
            ),
          ),
        ],
      )),
    );
  }
}
